package com.jiangyao.controller;

import com.jiangyao.common.ApiResult;
import com.jiangyao.common.utils.JsonUtil;
import com.jiangyao.model.dto.backend.ClassUserScoreEditDTO;
import com.jiangyao.model.dto.backend.ClassUserScoreListPageDTO;
import com.jiangyao.model.dto.backend.ClassUserScoreSemesterListDTO;
import com.jiangyao.model.dto.backend.ClassUserScoreSemesterQueryDTO;
import com.jiangyao.model.vo.backend.ClassUserScoreListVO;
import com.jiangyao.model.vo.backend.ClassUserScoreSemesterListVO;
import com.jiangyao.service.ClassUserScoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 学员学分信息 前端控制器
 * </p>
 *
 * @author: yangyongchuan
 * @create: 2020年08月30日 23:58
 */
@RestController
@RequestMapping("/classUserScore")
@Api(tags = "班级成员成绩管理(专业成绩)")
public class ClassUserScoreController extends BaseController {

	@Autowired
	private ClassUserScoreService classUserScoreService;


	/**
	 * 获取专业成绩学期课程列表
	 *
	 * @param listDTO 请求参数DTO
	 * @return 结果
	 */
	@ApiOperation(value = "获取专业成绩学期课程列表", notes = "必填：orgId，classId")
	@PostMapping("/semesterList")
	public ApiResult<List<ClassUserScoreSemesterListVO>> getSemesterList(@Validated({ClassUserScoreSemesterQueryDTO.ListPage.class}) @RequestBody ClassUserScoreSemesterQueryDTO listDTO) {
		return ApiResult.success(classUserScoreService.getSemesterList(listDTO));
	}

	/**
	 * 新建编辑专业成绩学期课程
	 *
	 * @param listDTO 请求参数DTO
	 * @return 结果
	 */
	@ApiOperation(value = "新建编辑专业成绩学期课程", notes = "批量新建、编辑")
	@PostMapping("/semesterList/update")
	public ApiResult<String> updateSemesterList(@Validated({ClassUserScoreSemesterListDTO.Edit.class}) @RequestBody ClassUserScoreSemesterListDTO listDTO) {
		classUserScoreService.updateSemesterList(listDTO);
		return new ApiResult<>("ok");
	}

	/**
	 * 删除专业成绩学期课程
	 *
	 * @param delDTO 删除DTO
	 * @return 结果
	 */
	@ApiOperation(value = "删除专业成绩学期课程")
	@PostMapping("/semesterList/delete")
	public ApiResult<String> deleteItemList(@Validated({ClassUserScoreSemesterQueryDTO.Delete.class}) @RequestBody ClassUserScoreSemesterQueryDTO delDTO) {
		classUserScoreService.deleteSemesterList(delDTO);
		return new ApiResult<>("ok");
	}


	/**
	 * 下载成绩导入模板
	 *
	 * @param response response
	 * @param dtoJson  班级成员成绩学分DTO json字符串
	 */
	@ApiOperation(value = "下载成绩导入模板")
	@ApiImplicitParam(name = "dtoJson", value = "班级成员成绩学分DTO json字符串", required = true, example = "{\"orgId\":1,\"classId\":1,\"semesterName\":\"第一学期\"}")
	@GetMapping("/downloadImportTemplate")
	public void downloadImportTemplate(HttpServletResponse response, @RequestParam String dtoJson) {
		classUserScoreService.downloadImportTemplate(response, dtoJson);
	}

	/**
	 * 导入成绩
	 *
	 * @param file 上传的文件
	 */
	@ApiOperation(value = "导入成绩")
	@ApiImplicitParam(name = "file", value = "上传的文件", required = true)
	@PostMapping(value = "/importScore")
	@ResponseBody
	public ApiResult<String> importUser(MultipartFile file) {
		if (file == null) {
			return ApiResult.error("文件不能为空");
		}
		classUserScoreService.importScore(file);
		return new ApiResult<>();
	}


	/**
	 * 获取班级成员专业成绩列表
	 *
	 * @param listPageDTO 列表DTO
	 * @return 结果
	 */
	@ApiOperation(value = "获取班级成员专业成绩列表")
	@PostMapping("/list")
	public ApiResult<ClassUserScoreListVO> getListPage(@Validated({ClassUserScoreListPageDTO.ListPage.class}) @RequestBody ClassUserScoreListPageDTO listPageDTO) {
		return ApiResult.success(classUserScoreService.getListPage(listPageDTO));
	}

	/**
	 * 导出班级成员专业成绩列表
	 *
	 * @param response    response
	 * @param listPageDTO 列表请求DTO json字符串
	 */
	@ApiOperation(value = "导出班级成员专业成绩列表", notes = "列表请求参数DTO转化为json字符串,json字符串还需要URL编码")
	@ApiImplicitParam(name = "listPageDTO", value = "列表请求DTO json字符串", required = true)
	@GetMapping("/export")
	public void export(HttpServletResponse response, @RequestParam String listPageDTO) {
		ClassUserScoreListPageDTO listPageDTOObj = JsonUtil.parse(listPageDTO, ClassUserScoreListPageDTO.class);
		classUserScoreService.export(response, listPageDTOObj);
	}

	/**
	 * 编辑成员专业成绩
	 *
	 * @param editDTO 编辑DTO
	 * @return 结果
	 */
	@ApiOperation(value = "编辑成员专业成绩")
	@PostMapping("/editUserScore")
	public ApiResult<String> editUserScore(@Validated({ClassUserScoreEditDTO.Edit.class}) @RequestBody ClassUserScoreEditDTO editDTO) {
		classUserScoreService.editUserScore(editDTO);
		return new ApiResult<>();
	}

	/**
	 * 获取专业成绩学期课程详情
	 *
	 * @param listDTO 请求参数DTO
	 * @return 结果
	 */
	@ApiOperation(value = "获取专业成绩学期课程详情", notes = "必填：orgId，classId，semesterName")
	@PostMapping("/semesterDetail")
	public ApiResult<ClassUserScoreSemesterListVO> getSemesterDetail(@Validated({ClassUserScoreSemesterQueryDTO.Detail.class}) @RequestBody ClassUserScoreSemesterQueryDTO listDTO) {
		return ApiResult.success(classUserScoreService.getSemesterDetail(listDTO));
	}

}
